package cn.iocoder.yudao.module.ai.service.creationcategory;

import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.ai.controller.admin.creationcategory.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationcategory.CreationCategoryDO;
import cn.iocoder.yudao.module.ai.dal.dataobject.creationmodel.CreationModelDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.ai.dal.mysql.creationcategory.CreationCategoryMapper;
import cn.iocoder.yudao.module.ai.dal.mysql.creationmodel.CreationModelMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.ai.enums.ErrorCodeConstants.*;

/**
 * 创作类别 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CreationCategoryServiceImpl implements CreationCategoryService {

    @Resource
    private CreationCategoryMapper creationCategoryMapper;
    @Resource
    private CreationModelMapper creationModelMapper;

    @Override
    public Long createCreationCategory(CreationCategorySaveReqVO createReqVO) {
        // 插入
        CreationCategoryDO creationCategory = BeanUtils.toBean(createReqVO, CreationCategoryDO.class);
        creationCategoryMapper.insert(creationCategory);
        // 返回
        return creationCategory.getId();
    }

    @Override
    public void updateCreationCategory(CreationCategorySaveReqVO updateReqVO) {
        // 校验存在
        validateCreationCategoryExists(updateReqVO.getId());
        // 更新
        CreationCategoryDO updateObj = BeanUtils.toBean(updateReqVO, CreationCategoryDO.class);
        creationCategoryMapper.updateById(updateObj);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCreationCategory(Long id) {
        // 校验存在
        validateCreationCategoryExists(id);
        // 删除
        creationCategoryMapper.deleteById(id);

        // 删除子表
        deleteCreationModelByCategoryId(id);
    }

    private void validateCreationCategoryExists(Long id) {
        if (creationCategoryMapper.selectById(id) == null) {
            throw exception(CREATION_CATEGORY_NOT_EXISTS);
        }
    }

    @Override
    public CreationCategoryDO getCreationCategory(Long id) {
        return creationCategoryMapper.selectById(id);
    }

    @Override
    public PageResult<CreationCategoryDO> getCreationCategoryPage(CreationCategoryPageReqVO pageReqVO) {
        return creationCategoryMapper.selectPage(pageReqVO);
    }

    // ==================== 子表（创作模型） ====================

    @Override
    public PageResult<CreationModelDO> getCreationModelPage(PageParam pageReqVO, Long categoryId) {
        return creationModelMapper.selectPage(pageReqVO, categoryId);
    }

    @Override
    public Long createCreationModel(CreationModelDO creationModel) {
        creationModelMapper.insert(creationModel);
        return creationModel.getId();
    }

    @Override
    public void updateCreationModel(CreationModelDO creationModel) {
        // 校验存在
        validateCreationModelExists(creationModel.getId());
        // 更新
        creationModelMapper.updateById(creationModel);
    }

    @Override
    public void deleteCreationModel(Long id) {
        // 校验存在
        validateCreationModelExists(id);
        // 删除
        creationModelMapper.deleteById(id);
    }

    @Override
    public CreationModelDO getCreationModel(Long id) {
        return creationModelMapper.selectById(id);
    }

    private void validateCreationModelExists(Long id) {
        if (creationModelMapper.selectById(id) == null) {
            throw exception(CREATION_MODEL_NOT_EXISTS);
        }
    }

    private void deleteCreationModelByCategoryId(Long categoryId) {
        creationModelMapper.deleteByCategoryId(categoryId);
    }

}